package com.nhjf.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

/**
 * 时间工具类
 * 
 * @author zhurz
 */
public class DateUtil {

	public final static SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public final static SimpleDateFormat DATE_TIME_FORMAT2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	public final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
	public final static SimpleDateFormat DATE_TIME_FORMAT3 = new SimpleDateFormat("yyyyMMddHHmmss");
	public final static SimpleDateFormat DATE_FORMAT2 = new SimpleDateFormat("yyyy-MM-dd");
	public final static SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HHmmss");

	

	/**
	 * 将日期字符串转换为 零点开始的日期(Date)对象
	 * 
	 * @param dateStr		日期字符串(yyyy-MM-dd)
	 * @return
	 */
	public static Date dateStr2BeginDate(String dateStr) {
		if(StringUtils.isNotBlank(dateStr)){
			try {
				return DATE_TIME_FORMAT2.parse(dateStr + " 00:00:00.000");
			} catch (ParseException e) { }
		}
		return null;
	}

	/**
	 * 将日期字符串转换为 59分59秒999毫秒结束的日期(Date)对象
	 * 
	 * @param dateStr		日期字符串(yyyy-MM-dd)
	 * @return
	 */
	public static Date dateStr2EndDate(String dateStr) {
		if(StringUtils.isNotBlank(dateStr)){
			try {
				return DATE_TIME_FORMAT2.parse(dateStr + " 23:59:59.999");
			} catch (ParseException e) { }
		}
		return null;
	}

	public static Date parseDateTime(String dateStr){
		try {
			return DATE_TIME_FORMAT.parse(dateStr);
		} catch (ParseException e) { }
		return null;
	}
	
	public static Date parseDate(String dateStr){
		try {
			return DATE_TIME_FORMAT2.parse(dateStr);
		} catch (ParseException e) { }
		return null;
	}
	
	public static String formatDateTime(Date date) {
		if (date != null) {
			return DATE_TIME_FORMAT.format(date);
		}
		return "";
	}
	
	public static String formatDateTime(Date date, String formatStr) {
		if (date != null) {
			if (!StringUtils.isBlank(formatStr)) {
				SimpleDateFormat format = new SimpleDateFormat(formatStr);
				return format.format(date);
			} else {
				return DATE_TIME_FORMAT.format(date);
			}
		}
		return "";
	}
	
	/**
	 * 格式化yyyMMdd
	 * @param dateStr
	 * @return
	 */
	public static Date parseDate2(String dateStr){
		try {
			if(StringUtils.isBlank(dateStr)){
				return null;
			}
			return DATE_FORMAT.parse(dateStr);
		} catch (ParseException e) { }
		return null;
	}
	/**
	 * 格式化yyyMMdd
	 * @param dateStr
	 * @return
	 */
	public static Date parseDate4(String dateStr){
		try {
			if(StringUtils.isBlank(dateStr)){
				return null;
			}
			return DATE_TIME_FORMAT3.parse(dateStr);
		} catch (ParseException e) { }
		return null;
	}
	
	/**
	 * YYYY-MM-DD
	 * @param dateStr
	 * @return
	 */
	public static Date parseDate3(String dateStr){
		try {
			return DATE_FORMAT2.parse(dateStr);
		} catch (ParseException e) { }
		return null;
	}
	/**
	 * HHmmss
	 * @param timeStr
	 * @return
	 */
	public static Date parseTime(String timeStr){
		try {
			return TIME_FORMAT.parse(timeStr);
		} catch (ParseException e) { }
		return null;
	}

	
}
